package com.ruoyi.web.StateDemo.service.impl;

import com.ruoyi.web.StateDemo.domin.ActivityOrder;
import com.ruoyi.web.StateDemo.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
/**
 * 订单业务处理状态
 */
public class ActivityStateServiceImpl implements OrderService {

    @Autowired
    private AllState.UnpayState unpayState;
    @Autowired
    private AllState.PaiedState paiedState;
    @Autowired
    private ContextState contextState;

    private final RuntimeException EXCEPTION=new RuntimeException("该状态不支持此操作");

    @Override
    public void payOrder(ActivityOrder order) {
        //查询当前订单的状态
        if(order.getOrderState()!=unpayState.getCurrentState().getValue()){
            throw  EXCEPTION;

        }
        contextState.setOrderState(unpayState);
        //调用要修改的状态方法
        contextState.paidEvent(contextState);

        //修改数据库状态
        System.out.println("方法调用完--数据库状态修改为:"+contextState.getCurrentState().toString());
    }

    @Override
    public void verificationOrder(ActivityOrder activityOrder) {
        //查询当前订单的状态
        if(activityOrder.getOrderState()!=paiedState.getCurrentState().getValue()){
            throw  EXCEPTION;

        }
        contextState.setOrderState(paiedState);
        //调用要修改的状态方法
        contextState.verificationEvent(contextState);

        //修改数据库状态
        System.out.println("方法调用完后--数据库状态修改为:"+contextState.getCurrentState().toString());
    }
}
